package org.xhnews.ui;

import java.util.Vector;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.VerticalFieldManager;

import org.xhnews.bo.WeatherBo;
import org.xhnews.to.WeatherItem;

public class WeatherField extends VerticalFieldManager implements ShowWindow{
	HorizontalFieldManager _fieldManagerTop = new HorizontalFieldManager();
	HorizontalFieldManager _fieldManagerMiddle = new HorizontalFieldManager();
	HorizontalFieldManager _fieldManagerBottom = new HorizontalFieldManager();
	BitmapField _today_Bitmap = new BitmapField(),
			_tomorrow_Bitmap = new BitmapField(),
			_aftertomorrow_Bitmap = new BitmapField();
	LabelField _today_Date = new LabelField(),
			_tomorrow_Date = new LabelField(),
			_aftertomorrow_Date = new LabelField();
	LabelField _today_WeatherDescription = new LabelField(),
			_tomorrow_WeatherDescription = new LabelField(),
			_aftertomorrow_WeatherDescription = new LabelField();
	LabelField _today_Temperature = new LabelField(),
			_tomorrow_Temperature = new LabelField(),
			_aftertomorrow_Temperature = new LabelField();
	LabelField _today_Wind = new LabelField(),
			_tomorrow_Wind = new LabelField(),
			_aftertomorrow_Wind = new LabelField();
	LabelField _today_DayInWeek = new LabelField(),
			_tomorrow_DayInWeek = new LabelField(),
			_aftertomorrow_DayInWeek = new LabelField();

	public WeatherField() {

		_fieldManagerTop.add(_today_Bitmap);
		_fieldManagerMiddle.add(_tomorrow_Bitmap);
		_fieldManagerBottom.add(_aftertomorrow_Bitmap);
		
		_fieldManagerTop.add(_today_Date);
		_fieldManagerMiddle.add(_tomorrow_Date);
		_fieldManagerBottom.add(_aftertomorrow_Date);
		
		_fieldManagerTop.add(_today_Temperature);
		_fieldManagerMiddle.add(_tomorrow_Temperature);
		_fieldManagerBottom.add(_aftertomorrow_Temperature);
		
//		_fieldManagerTop.add(_today_Wind);
//		_fieldManagerMiddle.add(_tomorrow_Wind);
//		_fieldManagerBottom.add(_aftertomorrow_Wind);
		
		_fieldManagerTop.add(_today_WeatherDescription);
		_fieldManagerMiddle.add(_tomorrow_WeatherDescription);
		_fieldManagerBottom.add(_aftertomorrow_WeatherDescription);

		add(_fieldManagerBottom);
		add(new SeparatorField());
		add(_fieldManagerMiddle);
		add(new SeparatorField());
		add(_fieldManagerTop);
		add(new SeparatorField());
		LabelField _details = new LabelField("详细信息", LabelField.FOCUSABLE
				| Field.USE_ALL_WIDTH) {
			protected void paint(Graphics graphics) {
					graphics.setColor(Color.BLUE);
				super.paint(graphics);
			}
			protected boolean navigationClick(int status, int time) {
				WeatherDetailScreen screen = new WeatherDetailScreen();
				UiApplication.getUiApplication().pushScreen(screen);
				return true;
			}
		};
        add(_details);
		initialize();
	}

	public void initialize() {
		reload();
	}
	public void reload(){
		Vector weatheritems = WeatherBo.getWeathers();
		if (weatheritems.size() != 0) {
			WeatherItem today_weather = (WeatherItem) weatheritems.elementAt(2);
			WeatherItem tomorrow_weather = (WeatherItem) weatheritems
					.elementAt(1);
			WeatherItem aftertomorrow_weather = (WeatherItem) weatheritems
					.elementAt(0);
			loadImg(today_weather, tomorrow_weather, aftertomorrow_weather);
			loadDate(today_weather, tomorrow_weather, aftertomorrow_weather);
			loadWeatherDescription(today_weather, tomorrow_weather,
					aftertomorrow_weather);
			loadTemperature(today_weather, tomorrow_weather,
					aftertomorrow_weather);
			loadWind(today_weather, tomorrow_weather, aftertomorrow_weather);
			loadDayInWeek(today_weather, tomorrow_weather,
					aftertomorrow_weather);
		}
	}

	private void loadImg(WeatherItem today_weather,
			WeatherItem tomorrow_weather, WeatherItem aftertomorrow_weather) {
		_today_Bitmap.setBitmap(Bitmap.getBitmapResource("b_"
				+ today_weather.getWeatherPicture1() + ".gif"));
		_tomorrow_Bitmap.setBitmap(Bitmap.getBitmapResource("b_"
				+ tomorrow_weather.getWeatherPicture1() + ".gif"));
		_aftertomorrow_Bitmap.setBitmap(Bitmap.getBitmapResource("b_"
				+ aftertomorrow_weather.getWeatherPicture1() + ".gif"));
	}

	private void loadDate(WeatherItem today_weather,
			WeatherItem tomorrow_weather, WeatherItem aftertomorrow_weather) {
		_today_Date.setText(today_weather.getDate(), 0, today_weather.getDate()
				.length());
		_tomorrow_Date.setText(tomorrow_weather.getDate(), 0, tomorrow_weather
				.getDate().length());
		_aftertomorrow_Date.setText(aftertomorrow_weather.getDate(), 0,
				aftertomorrow_weather.getDate().length());
	}

	private void loadWeatherDescription(WeatherItem today_weather,
			WeatherItem tomorrow_weather, WeatherItem aftertomorrow_weather) {
		_today_WeatherDescription.setText(
				today_weather.getWeatherDescription(), 0, today_weather
						.getWeatherDescription().length());
		_tomorrow_WeatherDescription.setText(tomorrow_weather
				.getWeatherDescription(), 0, tomorrow_weather
				.getWeatherDescription().length());
		_aftertomorrow_WeatherDescription.setText(aftertomorrow_weather
				.getWeatherDescription(), 0, aftertomorrow_weather
				.getWeatherDescription().length());
	}

	private void loadTemperature(WeatherItem today_weather,
			WeatherItem tomorrow_weather, WeatherItem aftertomorrow_weather) {
		_today_Temperature.setText(today_weather.getTemperature(), 0,
				today_weather.getTemperature().length());
		_tomorrow_Temperature.setText(tomorrow_weather.getTemperature(), 0,
				tomorrow_weather.getTemperature().length());
		_aftertomorrow_Temperature.setText(aftertomorrow_weather
				.getTemperature(), 0, aftertomorrow_weather.getTemperature()
				.length());
	}

	private void loadWind(WeatherItem today_weather,
			WeatherItem tomorrow_weather, WeatherItem aftertomorrow_weather) {
		_today_Wind.setText(today_weather.getWind(), 0, today_weather.getWind()
				.length());
		_tomorrow_Wind.setText(tomorrow_weather.getWind(), 0, tomorrow_weather
				.getWind().length());
		_tomorrow_Wind.setText(aftertomorrow_weather.getWind(), 0,
				aftertomorrow_weather.getWind().length());
	}

	private void loadDayInWeek(WeatherItem today_weather,
			WeatherItem tomorrow_weather, WeatherItem aftertomorrow_weather) {
//		_today_DayInWeek.setText(StringUtil.caculateDayOfWeek(StringUtil
//				.string2Date(today_weather.getDate())), 0, StringUtil
//				.caculateDayOfWeek(
//						StringUtil.string2Date(today_weather.getDate()))
//				.length());
//		_tomorrow_DayInWeek.setText(StringUtil.caculateDayOfWeek(StringUtil
//				.string2Date(tomorrow_weather.getDate())), 0, StringUtil
//				.caculateDayOfWeek(
//						StringUtil.string2Date(tomorrow_weather.getDate()))
//				.length());
//		_aftertomorrow_DayInWeek.setText(StringUtil
//				.caculateDayOfWeek(StringUtil.string2Date(aftertomorrow_weather
//						.getDate())), 0, StringUtil.caculateDayOfWeek(
//				StringUtil.string2Date(aftertomorrow_weather.getDate()))
//				.length());
	}
}
